<template>
  <div class="box">
    <el-row>
      <el-form label-position="left"
               inline
               class="form">
        <el-form-item label="监测点代码"
                      label-width="150px">
          {{form.mpCd}}
        </el-form-item>

        <el-form-item label="监测点名称"
                      label-width="150px">
          {{form.mpNm}}
        </el-form-item>

        <el-form-item label="所属取水户"
                      label-width="150px">
          <div class="name-item"
               :title="form.wrpNm">{{form.wrpNm}}</div>
        </el-form-item>

        <el-form-item label="设备类型"
                      label-width="150px">
          {{form.mpItemNm}}
        </el-form-item>

        <el-form-item label="水源类型"
                      label-width="150px">
          {{form.soupTpNm}}
        </el-form-item>

        <el-form-item label="监测点地址"
                      label-width="150px">
          {{form.mpAddr}}
        </el-form-item>
      </el-form>
    </el-row>

    <!-- 表格 -->
    <div class="table-box">
      <!-- 搜索栏 -->
      <el-date-picker v-model="search.date"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      :clearable="false">
      </el-date-picker>
      <el-button @click="handleSearch"
                 type="primary">查询</el-button>

      <el-button @click="exportExcel"
                 plain>导出</el-button>

      <el-table ref="table"
                :data="tableData"
                border
                highlight-current-row
                @current-change="tableCurrentChange"
                height="360px">
        <el-table-column label="时间"
                         prop="adddata">
        </el-table-column>
        <el-table-column label="时间(整点)"
                         prop="timebucket">
        </el-table-column>

        <el-table-column label="上报时间"
                         prop="systemtime"
                         min-width="120">
        </el-table-column>

        <el-table-column :label="form.mpItem==='1'?'高程水位(m)':'瞬时流量(m³/h)'"
                         prop="q"
                         min-width="120">
        </el-table-column>

        <el-table-column :label="form.mpItem==='1'?'绝对水位(m)':'累计水量(m³)'"
                         prop="totalq"
                         min-width="120">
        </el-table-column>

        <el-table-column v-if="form.mpItem!=='1'"
                         label="当前流速(m/s)"
                         prop="speed">
        </el-table-column>

        <el-table-column label="电压"
                         prop="voltage">
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pageNum"
                     :page-size="currentSize"
                     :page-sizes="[10, 20, 50, 100, 200]"
                     layout="total, prev, pager, next, sizes"
                     :total="total"></el-pagination>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  props: {
    mpCd: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      search: {
        date: ''
      },
      // 表单参数
      form: {
        mpCd: '',
        mpNm: '',
        wrpNm: '',
        soupTpNm: '',
        mpAddr: '',
      },
      // 表格参数
      loading: false,
      tableData: [],
      total: 0,
      pageNum: 1,
      currentSize: 50,
      currentRow: null
    }
  },
  methods: {
    // 获取选择器默认时间
    getDate() {
      let today = dayjs().format('YYYY-MM-DD');
      let nowTime = dayjs().format('YYYY-MM-DD HH:mm:ss');
      this.search.date = [`${today} 00:00:00`, nowTime]
    },
    

    // 表格选中行
    tableCurrentChange(row) {
      this.currentRow = row
    },

    // 分页
    handleSizeChange(size) {
      this.pageNum = 1;
      this.currentSize = size
      this.getTableData()
    },
    handleCurrentChange(index) {
      this.pageNum = index;
      this.getTableData()
    },
    // 通用监测点数据查询-上方数据
    getFormData() {
      this.$request({
        url: '/zy-api/api/common/getWaterIntakeRcdnm',
        method: 'post',
        data: {
          mpCd: this.mpCd
        }
      }).then(res => {
        if (res.status == 200) {
          this.form = res.data[0]
        } else {
          this.$message.error(`错误${res.status}；${res.errors[0].message}`)
        }
      })
    },
    // 获取表格数据
    getTableData() {
      this.loading = true
      let params = {
        pageIndex: this.pageNum,
        pageSize: this.currentSize,
        mn: this.mpCd,
        adddata: this.search.date[0],
        systemtime: this.search.date[1]
      }
      this.$request({
        url: '/zy-api/api/common/getWaterIntakeList',
        method: 'post',
        data: params
      }).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      }).finally(() => {
        if (this.$refs.table) {
          this.$refs.table.doLayout()
        }
        this.loading = false
      })
    },
    handleSearch(){
      this.pageNum = 1;
      this.getTableData();
    },
    // 导出表格
    exportExcel() {
      this.$utils.exportExcel('api/common/exportWaterIntake', {
        mn: this.mpCd,
        adddata: this.search.date[0],
        systemtime: this.search.date[1]
      })
    }
  },
  watch: {
    mpCd: {
      handler(val) {
        if (val) {
          this.getDate()
          this.getFormData()
          this.handleSearch()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getDate()
    this.getFormData()
    this.getTableData()
  },
}
</script>

<style lang="scss" scoped>
.box {
  .el-row {
    width: 99%;
    margin: 0 auto;
    margin-bottom: 20px;
  }
  .el-table {
    width: 100%;
    margin: 10px 0;
  }
  .form {
    border-right: 1px solid #dfe6ec;
    border-top: 1px solid #dfe6ec;
    .el-form-item {
      margin-right: 0;
      margin-bottom: 0;
      width: 50%;
      border-bottom: 1px solid #dfe6ec;
      border-left: 1px solid #dfe6ec;
      .name-item {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .table-box {
    width: 99%;
    margin: 0 auto;
    border: 1px solid #dfe6ec;
    padding: 10px;
    .el-button {
      margin-left: 20px;
    }
  }
}

/deep/ .el-form-item__label {
  background-color: #fffdf0;
  border-right: 1px solid #dfe6ec;
  padding-left: 10px;
  height: 100%;
}
/deep/ .el-form-item__content {
  padding-left: 10px;
  width: 65%;
  vertical-align: middle;
}
</style>